<template>
    <el-button plain @click="createInfoVisible = true">
       创建新的基金信息
    </el-button>
    <el-table :data="tableData" stripe :default-sort="{ prop: 'fundCode', order: 'descending' }" style="width: 100%">
        <el-table-column prop="fundCode" sortable :label="$t('fundCode')" />
        <el-table-column prop="fundName" :label="$t('fundName')" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="createTime" label="创建时间" />
    </el-table>

    <!-- 创建新的基金信息 -->
    <el-dialog v-model="createInfoVisible" title="Shipping address" width="500">
        <el-form :model="form">
            <el-form-item label="Promotion name" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="Zones" :label-width="formLabelWidth">
                <el-select v-model="form.region" placeholder="Please select a zone">
                    <el-option label="Zone No.1" value="shanghai" />
                    <el-option label="Zone No.2" value="beijing" />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="createInfoVisible = false">{{ $t('cancel') }}</el-button>
                <el-button type="primary" @click="createInfo()">
                    {{ $t('submit') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue';
import request from "../../utils/request";
import { ElMessage } from 'element-plus';
import { useI18n } from "vue-i18n"
const { t } = useI18n();

const createInfoVisible = ref(false)
const formLabelWidth = '140px'

const form = reactive({
    name: '',
    region: '',
    date1: '',
    date2: '',
    delivery: false,
    type: [],
    resource: '',
    desc: '',
})
//定义tableData
const tableData = ref<any[]>([]);
const getTableData = () => {
    request.get("/fund/v1/getFundsData").then(res => {
        if (res.data.code == 1) {
            tableData.value = res.data.data;
        } else {
            ElMessage.error(res.data.msg);
        }
    }).catch(err => {
        console.log("Error>>", err);
        ElMessage.error(t('networkError'));
    });
};
// 创建新的定投信息
const createInfo = () => {
    request.post("/fund/v1/createInfo", form).then(res => {
        if (res.data.code == 1) {
            ElMessage.success(res.data.msg);
            createInfoVisible.value = false;
            getTableData();
        } else {
            ElMessage.error(res.data.msg);
        }
    }).catch(err => {
        console.log("Error>>", err);
        ElMessage.error(t('networkError'));
    });
}
onMounted(() => {
    getTableData();
});
</script>